<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 工序SKU数据控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\StepService;
use think\Request;

class StepSkuController extends VerifyController
{
    /**
     * @var \common\service\StepService
     */
    protected $stepService;

    public function __construct()
    {
        parent::__construct();
        $this->stepService = StepService::getInstance();
    }

    /**
     * 生成工序的SKU
     * @param Request $request
     * @return \think\Response
     */
    public function generateStepSku(Request $request)
    {
        $param = $request->param();
        $resData = $this->stepService->generateStepSku($param['step_category_id']);
        return json(success_response('Generate was successful.', $resData));
    }

    /**
     * 保存指定工序Sku工时
     * @param Request $request
     * @return \think\Response|void
     * @throws \Exception
     */
    public function saveManHour(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->stepService->saveManHour($param['data']);
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }


    /**
     * 生成工序的SKU 通过工序id
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function generateStepSkuByStepId(Request $request)
    {
        $param = $request->param();
        $this->stepService->generateStepSkuByStepIdAsync($param['step_id']);
        return json(success_response('sku is generating in the background.'));
    }
}
